package com.stack.knowyouservice.service.impl;

import com.stack.knowyoubase.anotional.TimeOut;
import com.stack.knowyoubean.bean.HomeWork;
import com.stack.knowyoubean.bean.PreHomeWorkModel;
import com.stack.knowyoubase.constant.GlobalConst;
import com.stack.knowyoudao.dao.AnswerDao;
import com.stack.knowyoudao.dao.HomeWorkDao;
import com.stack.knowyouservice.service.DataAnalysisService;
import com.stack.knowyoubase.util.PreHomeWorkUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;



/**
 * 数据分析服务层接口
 *
 * @author stack
 */
@Service
@Scope("singleton")
public class DataAnalysisServiceImpl implements DataAnalysisService {

    @Resource
    private AnswerDao answerDao;
    @Resource
    private HomeWorkDao homeWorkDao;
    @Resource
    private PreHomeWorkUtil preHomeWorkUtil;

    @Transactional(readOnly = true, rollbackFor = Throwable.class)
    @TimeOut
    @Override
    public Map<String, Object> teacherScoreAnalysis(String param, Integer sid) {
        Map<String, Object> analysisMap = new HashMap<>(2);
        analysisMap.put("xData", null);
        analysisMap.put("yData", null);
        List<Map<String, Object>> analysisList;
        if (StringUtils.isEmpty(param)) {
            return analysisMap;
        }
        String[] paramType = {"scoreRate", "scoreRadio", "afterEnd", "labEnd"};
        int paramIndex1 = 0, paramIndex2 = 1, paramIndex3 = 2, paramIndex4 = 3;
        if (paramType[paramIndex1].equals(param)) {
            analysisList = answerDao.findScoreRate(sid);
            List<Object> xData = new ArrayList<>();
            List<Object> yData = new ArrayList<>();
            for (Map<String, Object> map : analysisList) {
                xData.add(map.get("xData"));
                yData.add(map.get("yData"));
            }
            analysisMap.put("xData", xData);
            analysisMap.put("yData", yData);
            return analysisMap;
        } else if (paramType[paramIndex2].equals(param)) {
            analysisList = answerDao.findHomeWorkRadio(null, sid);
            List<Object> xData = new ArrayList<>();
            List<Map<String, Object>> yData = new ArrayList<>();
            for (Map<String, Object> map : analysisList) {
                xData.add(map.get("xData"));
                Map<String, Object> yDataMap = new HashMap<>(2);
                yDataMap.put("value", map.get("yData"));
                yDataMap.put("name", map.get("xData"));
                yData.add(yDataMap);
            }
            analysisMap.put("xData", xData);
            analysisMap.put("yData", yData);
            return analysisMap;

        } else if (paramType[paramIndex3].equals(param)) {
            analysisList = answerDao.findEndAnswerCount(sid, GlobalConst.HOMEWORK_AFTER);
            List<Object> xData = new ArrayList<>();
            List<Object> yData = new ArrayList<>();
            for (Map<String, Object> map : analysisList) {
                xData.add(map.get("xData"));
                yData.add(map.get("yData"));
            }
            analysisMap.put("xData", xData);
            analysisMap.put("yData", yData);
            return analysisMap;
        } else if (paramType[paramIndex4].equals(param)) {
            analysisList = answerDao.findEndAnswerCount(sid, GlobalConst.HOMEWORK_LAB);
            List<Object> xData = new ArrayList<>();
            List<Object> yData = new ArrayList<>();
            for (Map<String, Object> map : analysisList) {
                xData.add(map.get("xData"));
                yData.add(map.get("yData"));
            }
            analysisMap.put("xData", xData);
            analysisMap.put("yData", yData);
            return analysisMap;
        } else {
            return analysisMap;
        }
    }

    @Transactional(readOnly = true, rollbackFor = Throwable.class)
    @TimeOut
    @Override
    public Map<String, Object> stuScoreAnalysis(Integer hkid, Integer sid, Authentication authentication) {
        Map<String, Object> analysisMap = new HashMap<>(2);
        analysisMap.put("xData", null);
        analysisMap.put("yData", null);
        List<Map<String, Object>> analysisList;

        if (GlobalConst.HOMEWORK_AFTER.equals(hkid) || GlobalConst.HOMEWORK_LAB.equals(hkid)) {
            analysisList = answerDao.findHomeWorkScore(hkid, sid, Integer.parseInt(authentication.getName()));
            List<Object> xData = new ArrayList<>();
            List<Object> yData = new ArrayList<>();
            for (Map<String, Object> map : analysisList) {
                xData.add(map.get("xData"));
                yData.add(map.get("yData"));
            }
            analysisMap.put("xData", xData);
            analysisMap.put("yData", yData);
            return analysisMap;
        } else if (GlobalConst.HOMEWORK_ALL.equals(hkid)) {
            analysisList = answerDao.findHomeWorkRadio(Integer.parseInt(authentication.getName()), sid);
            List<Object> xData = new ArrayList<>();
            List<Map<String, Object>> yData = new ArrayList<>();
            for (Map<String, Object> map : analysisList) {
                xData.add(map.get("xData"));
                Map<String, Object> yDataMap = new HashMap<>(2);
                yDataMap.put("value", map.get("yData"));
                yDataMap.put("name", map.get("xData"));
                yData.add(yDataMap);
            }
            analysisMap.put("xData", xData);
            analysisMap.put("yData", yData);
            return analysisMap;

        } else {
            return analysisMap;
        }
    }

    @Transactional(readOnly = true, rollbackFor = Throwable.class)
    @TimeOut
    @Override
    public Map<String, Object> examAnalysis(Integer hid, Integer sid) {
        //解决响应层空指针异常
        Map<String, Object> scoreRangeMap = new HashMap<>(2);
        scoreRangeMap.put("scoreRange", null);
        scoreRangeMap.put("resultList", null);
        //判断是否拥有作业权限
        List<Integer> homeWorkBySid = homeWorkDao.findHomeWorkBySid(sid);
        boolean contains = homeWorkBySid.contains(hid);
        if (!contains) {
            return scoreRangeMap;
        } else {
            List<Map<String, Object>> answerScore = answerDao.findAnswerScore(hid);
            if (answerScore.isEmpty()) {
                return scoreRangeMap;
            } else {
                List<String> scoreRange = new ArrayList<>();
                for (Map<String, Object> map : answerScore) {
                    if (map.get("name") != null) {
                        scoreRange.add(((Double) map.get("name")).toString());
                    }
                }
                scoreRangeMap.put("scoreRange", scoreRange);
                scoreRangeMap.put("resultList", answerScore);
                return scoreRangeMap;
            }
        }
    }

    @Transactional(readOnly = true, rollbackFor = Throwable.class)
    @TimeOut
    @Override
    public Map<String, Object> preHomeWorkAnalysis(Integer hid, Integer sid) {
        //解决响应层空指针异常
        Map<String, Object> rateMap = new HashMap<>(2);
        rateMap.put("keyList", null);
        rateMap.put("rateList", null);
        //判断是否拥有作业权限
        List<Integer> homeWorkBySid = homeWorkDao.findHomeWorkBySid(sid);
        boolean contains = homeWorkBySid.contains(hid);
        if (!contains) {
            return rateMap;
        } else {
            List<String> preAnswer = answerDao.findPreAnswer(hid);
            if (preAnswer.isEmpty()) {
                return null;
            } else {
                HomeWork homeWorkById = homeWorkDao.findHomeWorkById(hid);
                List<PreHomeWorkModel> preHomeWorkModels1 = preHomeWorkUtil.modelCreate(homeWorkById.getHcontent());
                //关键词集合
                List<String> keyList = new ArrayList<>();
                for (PreHomeWorkModel preHomeWorkModel : preHomeWorkModels1) {
                    keyList.add(preHomeWorkModel.getKey());
                }
                rateMap.put("keyList", keyList);
                //初始化评分数组，6个评分行，关键词列
                int[][] rateList = new int[6][keyList.size()];
                for (String answer : preAnswer) {
                    List<PreHomeWorkModel> preHomeWorkModels = preHomeWorkUtil.modelCreate(answer);
                    for (int i = 0; i < keyList.size(); i++) {
                        PreHomeWorkModel preHomeWorkModel = preHomeWorkModels.get(i);
                        rateList[preHomeWorkModel.getRate()][i] = rateList[preHomeWorkModel.getRate()][i] + 1;
                    }
                }
                rateMap.put("rateList", rateList);
                return rateMap;
            }
        }
    }
}
